package com.rwb.springboot;

import com.rwb.springboot.springbootapi.MyFilter2;
import com.rwb.springboot.springbootapi.MyServlet2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.DispatcherType;

/**
 * Created by ruwenbo on 2018/4/18.
 */
@SpringBootApplication
@ServletComponentScan(basePackages = "com.rwb.springboot")
public class Lesson4Application {

    public static void main(String[] args) {

        SpringApplication.run(Lesson4Application.class, args);
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        ServletRegistrationBean registrationBean = new ServletRegistrationBean();

        registrationBean.setServlet(new MyServlet2());
        registrationBean.setName("myServlet2");
        registrationBean.addUrlMappings("/MyServlet2");
        registrationBean.addInitParameter("myname", "myValue");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();

        registrationBean.setFilter(new MyFilter2());
        registrationBean.addServletNames("myServlet2");
        registrationBean.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST, DispatcherType.INCLUDE);

        return registrationBean;
    }
}
